import { h } from "koishi"

export const name = "衋报"

export function apply(ctx) {
  function isPassive(session) {
    if (typeof session.onebot?.sub_type === "string") return session.onebot.sub_type !== "leave"
    return session.subtype === "passive"
  }

  ctx.on("guild-member-removed", async session => {
    const { assignee, locales } = await session.observeChannel(["assignee", "locales"])
    if (session.selfId !== assignee) return ctx.logger.debug("not assignee, skip", session)
    const { name, avatar } = await session.bot.getUser(session.userId).catch(() => null) || {}
    const paths = ["lnnbot.衋报.default"]
    if (isPassive(session)) paths.unshift("lnnbot.衋报.passive")
    const msg = ctx.i18n.render(locales, paths, { name: name || session.userId, id: session.userId })
    if (avatar) {
      const u = new URL(avatar)
      u.searchParams.set("spec", "100")
      msg.push(h.image(u.href))
    }
    session.bot.sendMessage(session.channelId, msg)
  })

  ctx.i18n.define("zh-CN", "lnnbot.衋报.default", "衋报：{name} ({id}) 郁代了")
  ctx.i18n.define("zh-CN", "lnnbot.衋报.passive", "{name} ({id}) 被移出群聊")
}
